import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:graduation_project/cartoon_module/cartoon_list_three.dart';
import 'dart:convert' as convert;

class screen_body extends StatefulWidget {
  const screen_body({Key key}) : super(key: key);

  @override
  State<screen_body> createState() => _screen_bodyState();
}

class _screen_bodyState extends State<screen_body> {
  List cartoon_list0 = [
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai11',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai21',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai31',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai41',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai51',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai61',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai71',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai81',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai91',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai101',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai111',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai121',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
  ];

  List cartoon_list1 = [
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai1',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai2',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai3',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai4',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai5',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai6',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai7',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai8',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai9',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai10',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai11',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
    {
      'cartoon': 1007,
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'name': 'haihaihai12',
      'author': '尾田荣一郎',
      'tage': 'rexue',
      'stae': '连载中'
    },
  ];
  int dou_page = 0;
  int tage_num = 0;
  List car_listRes = [];
  List cartoon_tage = [
    {'tage_name': '全部', 'state': 1},
    {'tage_name': '热血', 'state': 0},
    {'tage_name': '古风', 'state': 0},
    {'tage_name': '科幻', 'state': 0},
    {'tage_name': '都市', 'state': 0},
    {'tage_name': '青春', 'state': 0},
    {'tage_name': '穿越', 'state': 0},
    {'tage_name': '未来', 'state': 0},
    {'tage_name': '悬疑', 'state': 0},
    {'tage_name': '灵异', 'state': 0},
    {'tage_name': '现代', 'state': 0},
    {'tage_name': '恋爱', 'state': 0},
  ];

  List sortord_list = [
    {'sortord_name': '人气推荐', 'state': 1},
    {'sortord_name': '更新时间', 'state': 0},
    {'sortord_name': '追漫人数', 'state': 0},
    {'sortord_name': '上架时间', 'state': 0},
  ];

  Future _future;

  _getinformation() async {
    dou_page++;
    Response response;
    var dio = Dio();
    dio.options.responseType = ResponseType.plain;
    response = await dio.get(
        'http://119.91.231.198:3033/getCarTage?tage_id=' + tage_num.toString());
    var res = convert.jsonDecode(response.toString());
    // for (var i = 12 * (dou_page - 1); i < 12 * dou_page; i++) {
    //   // print(res['contentArr']);
    //   car_listRes.add(res['contentArr'][i]);
    // }
    // car_listRes = null;
    for (var i = 0; i < res['contentArr'].length; i++) {
      // print(res['contentArr']);
      car_listRes.add(res['contentArr'][i]);
    }
    // print(res['contentArr'][0]);
  }
// listview 上拉刷新 下拉加载

  ScrollController _scrollController = new ScrollController();
  ScrollController _scrollController_num = new ScrollController();

  @override
  void initState() {
    print('进入分类');
    super.initState();

    _future = _getinformation();

    // 下拉加载
    // _scrollController = new ScrollController();
    _scrollController_num.addListener(() {});
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        // _onLoadmore();
        print('滑到了最底部');
        // cartoon_list1..addAll(cartoon_list0);
        print(cartoon_list0.length);

        setState(() {
          // _getinformation();
        });
      }

      // print(_scrollController.offset);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    // controller.stop();
    // 离开时关闭
    _scrollController.dispose();
    print('离开分类');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return NestedScrollView(
        controller: _scrollController_num,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              // leading: null,
              automaticallyImplyLeading: false,
              backgroundColor: Color.fromRGBO(255, 250, 250, 1),
              expandedHeight: 100.0,
              pinned: false,
              floating: true,
              snap: true,
              // ignore: missing_return
              flexibleSpace: OverflowBox(
                // padding: EdgeInsets.only(top: 10),
                // margin: EdgeInsets.only(top: 80),
                maxHeight: 120,
                child: Column(
                  children: [
                    Expanded(child: SizedBox()),
                    Wrap(
                      spacing: 10,
                      children: cartoon_tage
                          .asMap()
                          .keys
                          .map(((v) => InkWell(
                                onTap: () {
                                  cartoon_tage.forEach((element) {
                                    // print(element['state']);
                                    element['state'] = 0;
                                  });
                                  // v['state'] = 1;
                                  tage_num = v;
                                  cartoon_tage[v]['state'] = 1;

                                  setState(() {});
                                },
                                child: Container(
                                  margin: EdgeInsets.only(bottom: 4, top: 4),
                                  child: Text(
                                    cartoon_tage[v]['tage_name'],
                                    // '1',
                                    style: TextStyle(
                                        color: cartoon_tage[v]['state'] == 1
                                            ? Color.fromRGBO(80, 200, 255, 1)
                                            : Colors.black87),

                                    // backgroundColor: Color(0xFFf2f2f2),
                                  ),
                                ),
                              )))
                          .toList(),
                    ),
                    Container(
                      height: 10.0,
                    ),
                    Container(
                      height: 30.0,
                    ),
                  ],
                ),
              ),
            ),
          ];
        },
        body: FutureBuilder(
          future: _future,
          builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              print(car_listRes);
              return RefreshIndicator(
                onRefresh: () async {
                  //模拟网络请求
                  print('上拉刷新');
                  // print(cartoon_list0);
                  await Future.delayed(Duration(milliseconds: 2000));
                  //结束刷新
                  return Future.value(true);
                },
                child: ListView(
                  // controller: _scrollController,

                  children: [
                    Container(
                      alignment: Alignment.center,
                      child: cartoon_list_three(
                        cartoon_list: car_listRes,
                      ),
                    )
                  ],
                  // controller: _scrollController,
                ),
              );
            } else {
              return Container();
            }
          },
        ));
  }
}
